var configDefault = {
    // 线上微信费率
    0:[
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'今天天气不错',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'明天天气不错',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'后天天气不错',update_time:'2020-02-06 15:20',rate_value:'0.006'}
    ],
    1:[
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'}
    ],
    2:[
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'}
    ],
    3:[
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'}
    ],
    4:[
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'}
    ],
    5:[
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'},
        {begin_time:'2020-02-06 15:20',end_time:'2020-02-06 15:20',remark:'2020-02-06 15:20',update_time:'2020-02-06 15:20',rate_value:'0.006'}
    ]
}
var titleArr = [
    '线上微信费率',
    '线上支付宝费率',
    '线下微信费率',
    '线下支付宝费率',
    '线下银联京东费率',
    '线下云联云闪付费率'
]
var cols = [[
    {field:'rate_value',title:'费率',align:'center'},
    {field:'',title:'生效起止日期',align:'center',templet:function(d){
        return d.begin_time+' ~ '+d.end_time;
    }},
    {field:'remark',title:'备注',align:'center'},
    {field:'update_time',title:'更新时间',align:'center'},
    {title:'操作',align:'center',templet:function(d){
        return `
                <button type="button" data-type="edit" data-arrindex = ${d.arrindex} data-index=${d.LAY_TABLE_INDEX} class="table-operator-btn layui-btn layui-btn-sm layui-btn-normal">编辑</button>
                <button type="button" data-type="delete" data-arrindex = ${d.arrindex} data-index=${d.LAY_TABLE_INDEX} class="table-operator-btn layui-btn layui-btn-sm layui-btn-primary">删除</button>
                `;
    }}
]]
var table,layer,laydate,form;
layui.use(['table', 'layer','laydate'],function(){
    table = layui.table;
    layer = layui.layer;
    laydate = layui.laydate;
    form = layui.form;
    initTable(configDefault);
    // 表格中的按钮事件
    $(document).on('click','.table-operator-btn',function(){
        var type = $(this).data('type');
        var arrindex = $(this).data('arrindex');
        var index = $(this).data('index');
        if(type === 'delete'){
            // 删除
            layer.confirm('确定删除吗',{
                btn:['确定','取消']
            },function(i){
                // 删除相应位置的数组元素
                configDefault[arrindex].splice(index,1);
                initTable(configDefault);
                layer.close(i);
            },function(){
                console.log('取消')
            })
        }else{
            // 编辑  arrindex为undefined时表示增加
            // 当前操作行的数据
            var trData = arrindex !== undefined ? configDefault[arrindex][index] : {rate_value:'',begin_time:'',end_time:'',remark:''} ;
            layer.open({
                type:1,
                content:$("#editTemplet").html(),
                btn:['确定','取消'],
                btnAlign:'c',
                area:['400px','500px'],
                yes:function(index){
                    layer.close(index);
                },
                btn2:function(){

                },
                success:function(){
                    laydate.render({
                        elem: '#dateBeginInput'
                    });
                    laydate.render({
                        elem: '#dateEndInput'
                    });
                    // 给表单赋值
                    form.val('rateEditForm',{
                        rate_value: trData['rate_value'],
                        begin_time: trData['begin_time'],
                        end_time: trData['end_time'],
                        remark: trData['remark']
                    })
                }
            }) 
        }
    })
})
// 根据配置项 生成配置表格
function initTable(data){
    for(let key in data){
        var html = '<div class="rate-wrap-item">';
        data[key].forEach(function(item){
            item.arrindex = key;
        })
        html += '<div class="title">'+titleArr[key]+'<button data-type="add" class="table-operator-btn add-rate layui-btn layui-btn-sm layui-btn-normal" data-index='+key+'>+添加费率</button></div>';
        html += '<div>';
        html += '<table id="rateSetTable'+key+'"></table>'
        html += '</div>';
        html += '</div>';
        $(".rate-wrap").append(html);
        renderTable("rateSetTable"+key,data[key]); 
    }
}
function renderTable(id,data){
    console.log(id)
    table.render({
        elem:'#'+id,
        cols:cols,
        data:data,
        done:function(){
            console.log('渲染完毕')
        }
    })
}